package com.devilloki.viki.overspeedalert.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.devilloki.viki.overspeedalert.Events.GpsInfoEvent;
import com.devilloki.viki.overspeedalert.R;
import com.devilloki.viki.overspeedalert.tools.MY_NAMES;
import com.devilloki.viki.overspeedalert.tools.SettingsHelper;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**显示行驶时间的fragment
 * Created by viki on 2016/3/24.
 */
public class DisplayTrackTimeFragment extends Fragment {
    /**
     * 显示行驶时间的TextView
     */
    private TextView tvTrackTime=null;
    /**
     * 用于在SharedPreferences存储数据的工具类
     */
    private SettingsHelper setting =null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.display_track_time, container, false);
        setting = new SettingsHelper(this.getActivity());
        tvTrackTime = (TextView) view.findViewById(R.id.tv_track_time);
        if(null!=savedInstanceState){
            tvTrackTime.setText(
                    savedInstanceState.getString(
                            MY_NAMES.KEY_START_TIME,MY_NAMES.DATA_Default_LngAndLat));
        }
        return view;
    }
    /**
     * 根据位置位置的变化更新界面
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEventDisplayTrackTime(GpsInfoEvent event) {

        //行程的平均速度
        double trackStartTime=Double.valueOf(setting.getString(MY_NAMES.KEY_START_TIME,MY_NAMES.DATA_Default_LngAndLat));
        if(trackStartTime>0){
            // 如果有起始时间,则计算平均速度
            double trackTime=System.currentTimeMillis()
                    -Double.valueOf(setting.getString(MY_NAMES.KEY_START_TIME,MY_NAMES.DATA_Default_LngAndLat));
            tvTrackTime.setText(getString(R.string.track_time)+formatMS((long) trackTime));

        }


    }


    /**
     * 将毫秒格式化
     * @param paramMS 需要格式化的毫秒
     * @return 格式化为 HH:mm:ss 的字符串
     */
    private   String formatMS(long paramMS){

        long day=paramMS/(24*60*60*1000);
        long hour=(paramMS/(60*60*1000)-day*24);
        long min=((paramMS/(60*1000))-day*24*60-hour*60);
        long s=(paramMS/1000-day*24*60*60-hour*60*60-min*60);

        String str="%02d"+this.getString(R.string.time_hour)
                +"%02d"+this.getString(R.string.time_min)
                +"%02d"+this.getString(R.string.time_s);
        return String.format(str,hour,min,s);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(MY_NAMES.KEY_START_TIME,
                String .valueOf(tvTrackTime.getText()));
    }
}
